﻿using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System;
using System.Collections.Generic;

namespace DTU_Test;

/// <summary>
/// 全局帮助类
/// </summary>
public static partial class Helper
{
    /// <summary>
    /// 反序列化
    /// </summary>
    /// <typeparam name="T"> </typeparam>
    /// <param name="value"> </param>
    /// <returns> </returns>
    public static T? DeSerializeObject<T>(string? value)
    {
        try
        {
            if (string.IsNullOrEmpty(value))
            {
                return default(T);
            }

            return JsonConvert.DeserializeObject<T>(value);
        }
        catch (Exception ex)
        {
        }
        return default(T);
    }

    /// <summary>
    /// 序列化成字符串
    /// </summary>
    /// <typeparam name="T"> </typeparam>
    /// <param name="value"> </param>
    /// <returns> </returns>
    public static string? SerializeObject<T>(T value)
    {
        try
        {
            IsoDateTimeConverter timeFormat = new IsoDateTimeConverter();
            timeFormat.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
            string json = JsonConvert.SerializeObject(value, Formatting.None, timeFormat);
            return json;
        }
        catch (Exception ex)
        {
        }
        return null;
    }

    /// <summary>
    /// 将对象序列化为JSON格式字符串
    /// </summary>
    /// <param name="o"></param>
    /// <returns></returns>
    public static string ToJson(this object o)
    {
        IsoDateTimeConverter timeFormat = new IsoDateTimeConverter();
        timeFormat.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
        string json = JsonConvert.SerializeObject(o, Formatting.None, timeFormat);
        return json;
    }

    /// <summary>
    /// 解析JSON字符串生成对象实体
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="json"></param>
    /// <returns></returns>
    public static T? FromJson<T>(this string? json)
    {
        try
        {
            if (string.IsNullOrEmpty(json))
            {
                return default(T);
            }

            return JsonConvert.DeserializeObject<T>(json);
        }
        catch (Exception ex)
        {
            return default(T);
        }
    }

    /// <summary>
    /// 无符号短整数转成字节数组
    /// </summary>
    /// <param name="value"></param>
    /// <returns></returns>
    public static byte[] ToBytes(this ushort value)
    {
        byte[] buff = BitConverter.GetBytes(value);
        if (!BitConverter.IsLittleEndian)
        {
            Array.Reverse(buff);
        }
        return buff;
    }

    /// <summary>
    /// 字节数组转换成字符串
    /// </summary>
    /// <param name="value"></param>
    /// <returns></returns>
    public static string ToHexString(this IList<byte> value)
    {
        string tem = "";
        if (value != null)
        {
            foreach (byte b in value)
            {
                tem += b.ToString("X2");
            }
        }
        return tem;
    }

    /// <summary>
    /// modbus crc16校验
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    public static byte[] ToCRCModbus(this byte[] data)
    {
        int crc = 0xffff;
        for (int i = 0; i < data.Length; i++)
        {
            crc ^= data[i];
            for (int j = 0; j < 8; j++)
            {
                int temp = crc & 1;
                crc >>= 1;
                if (temp == 1)
                {
                    crc ^= 0xa001;
                }
            }
        }
        byte[] crc16 = new byte[2];
        crc16[1] = (byte)((crc >> 8) & 0xff);
        crc16[0] = (byte)(crc & 0xff);
        return crc16;
    }
}